#include "stm32f10x.h"
#include "key.h"
#include "lcd.h"
#include "led.h"
#include "adc.h"
#include "i2c.h"
#include "stdio.h"

u32 TimingDelay = 0;
u8 io1_fre = 2;
u8 io2_fre = 1;
u8 io1_n = 1;
u8 io2_n = 1;
//Main Body
int main(void)
{
	u8 key_val = 0;
	float adc_val1 = 0;
	float adc_val2 = 0;
	char txt[40];
	SysTick_Config(SystemCoreClock/1000);
	led_init();
	STM3210B_LCD_Init();
	LCD_SetTextColor(White);
	LCD_SetBackColor(Blue2);
	LCD_Clear(Blue2);
	i2c_init();
	key_init();
	adc_init();
	capture();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	io1_n = read_data(1);
	io2_n = read_data(2);
	
//	LCD_DisplayStringLine(Line4,"Xin");
	while(1)
	{
		key_val = get_state();
		
		switch(key_val){
			case page1:{
				led_ctrl(led8,1);
				adc_val1 = get_adc1();
				adc_val2 = get_adc2();
				if(adc_val1>adc_val2)
					led_ctrl(led8,0);
				else
					led_ctrl(led8,1);
				sprintf(txt,"     Measuring");
				LCD_DisplayStringLine(Line1,txt);
				sprintf(txt,"PULS1: %dKHz   ",io1_fre);
				LCD_DisplayStringLine(Line3,txt);
				sprintf(txt,"PULS2: %dKHz   ",io2_fre);
				LCD_DisplayStringLine(Line4,txt);
				sprintf(txt,"VOL1: %.2lfV",adc_val1);
				LCD_DisplayStringLine(Line5,txt);
				sprintf(txt,"VOL2: %.2lfV",adc_val2);
				LCD_DisplayStringLine(Line6,txt);
			}break;
			case page2:{
				led_ctrl(led8,0);
				sprintf(txt,"     Setting");
				LCD_DisplayStringLine(Line1,txt);
				sprintf(txt,"DIV1:  %.1d     ",io1_n);
				LCD_DisplayStringLine(Line3,txt);
				sprintf(txt,"Times: %d    ",io2_n);
				LCD_DisplayStringLine(Line4,txt);
				LCD_ClearLine(Line5);
				LCD_ClearLine(Line6);
			}break;
			case set_n1:{
				sprintf(txt,"     Setting");
				LCD_DisplayStringLine(Line1,txt);
				LCD_SetTextColor(Red);
				sprintf(txt,"DIV1:  %.1d    ",io1_n);
				LCD_DisplayStringLine(Line3,txt);
				LCD_SetTextColor(White);
				sprintf(txt,"Times: %d    ",io2_n);
				LCD_DisplayStringLine(Line4,txt);
				LCD_ClearLine(Line5);
				LCD_ClearLine(Line6);
			}break;
			case set_n2:{
				sprintf(txt,"     Setting");
				LCD_DisplayStringLine(Line1,txt);
				sprintf(txt,"DIV1:  %.1d     ",io1_n);
				LCD_DisplayStringLine(Line3,txt);
				LCD_SetTextColor(Red);
				sprintf(txt,"Times: %d    ",io2_n);
				LCD_DisplayStringLine(Line4,txt);
				LCD_SetTextColor(White);
			}break;
		}
	}
}

//

